#ifndef __MUTEX__HPP__
#define __MUTEX__HPP__
#include <iostream>
#include <string>
#include <pthread.h>

namespace wxd
{
    class Mutex
    {
    public:
        Mutex()
        {
            // 初始化锁
            pthread_mutex_init(&_mutex, nullptr);
        }
        ~Mutex()
        {
            // 释放锁
            pthread_mutex_destroy(&_mutex);
        }
        void lock()
        {
            pthread_mutex_lock(&_mutex);
        }

        void unlock()
        {
            pthread_mutex_unlock(&_mutex);
        }
        pthread_mutex_t* Get()
        {
            return &_mutex;
        }

    private:
        pthread_mutex_t _mutex;
    };

    class LockGuard
    {
    public:
        LockGuard(Mutex *mutex)
            : _mutex(mutex)
        {
            // 构造的时候就加锁
            _mutex->lock();
        }
        ~LockGuard()
        {
            _mutex->unlock();
            // 析构的时候就解锁
        }
        

    private:
        Mutex *_mutex;
    };
}

#endif